package hl.demosample.data.repository

import com.google.gson.Gson
import hl.demosample.app.network.apiService
import hl.demosample.data.model.bean.ApiResponse
import hl.demosample.data.model.bean.UserInfo
import hl.demosample.data.model.paramsrequest.LoginAndRegisterParamsRequest
import me.hgj.jetpackmvvm.network.AppException
import okhttp3.MediaType
import okhttp3.RequestBody

/*
* path: DemoSample 
* Author: hl
* Create: 2021-08-18 14:17
* Changes (from 2021-08-18)
* Desc: 请求套请求
*/

val HttpRequestCoroutine: HttpRequestManger by lazy(mode = LazyThreadSafetyMode.SYNCHRONIZED) {
    HttpRequestManger()
}

class HttpRequestManger {

    /*** 注册并登陆 ***/
    suspend fun register(username: String, password: String): ApiResponse<UserInfo> {
        val map = HashMap<String, String>()
        map["mobile"] = username
        map["password"] = password
        map["sex"] = "男"
        val registerData = apiService.register(map)
        //判断注册结果 注册成功，调用登录接口
        if (registerData.isSucces()) {
            return apiService.login(username, password)
        } else {
            //抛出错误异常
            throw AppException(registerData.code, registerData.msg)
        }
    }
}
